home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 038a / ike.zip / TOOLBOX.C < prev   
C/C++ Source or Header  |  1991-01-08  |  12KB  |  348 lines

  1. /*===========================================================================*/
  2. /*                                                                           */
  3. /* File    : TOOLBOX.C                                                       */
  4. /*                                                                           */
  5. /* Purpose : Implements a Windows "toolbox" control class.                   */
  6. /*                                                                           */
  7. /* History :                                                                 */
  8. /*                                                                           */
  9. /* (C) Copyright 1991      Marc Adler/Magma Systems     All Rights Reserved  */
  10. /*===========================================================================*/
  11.  
  12. #include <memory.h>
  13. #include <windows.h>
  14. #include "ico.h"
  15.  
  16.  
  17. HWND hWndTool;       /* drawing-tool toolbox  */
  18. TOOLBOX ToolBox =
  19. {
  20.   2, 40, 40
  21. };
  22.  
  23. HWND hWndColor;      /* color palette toolbox */
  24. TOOLBOX ColorBox =
  25. {
  26.   2, 40, 40
  27. };
  28.  
  29.  
  30. /****************************************************************************/
  31. /*                                                                          */
  32. /* Function : ToolboxInit()                                                 */
  33. /*                                                                          */
  34. /* Purpose  : Initializes the two toolboxes which IKE uses.                 */
  35. /*                                                                          */
  36. /* Returns  : TRUE if successful, FALSE if not.                             */
  37. /*                                                                          */
  38. /****************************************************************************/
  39. BOOL PASCAL ToolboxInit(HWND hWnd)
  40. {
  41.   HWND hButtonTool, hButtonColor;
  42.   int  i;
  43.  
  44.   /*
  45.     Create the drawing-tools toolbox
  46.   */
  47.   hWndTool = CreateWindow("ToolBox",
  48.                           (LPSTR) "Drawing Tools",
  49.                           WS_CHILD | WS_BORDER | WS_CAPTION | 
  50.                           WS_OVERLAPPED | WS_VISIBLE | WS_CLIPSIBLINGS,
  51.                           5, 272,
  52.                           ToolBox.cxButton*4 + 2*GetSystemMetrics(SM_CXBORDER),
  53.                           ToolBox.cyButton*2 + GetSystemMetrics(SM_CYCAPTION)
  54.                                              + 2*GetSystemMetrics(SM_CYBORDER),
  55.                           hWnd,
  56.                           100,
  57.                           hInst,
  58.                           (LPSTR) &ToolBox);
  59.   if (!hWndTool)
  60.   {
  61.     MessageBox(hWnd, "Can't create hWndTool", NULL, MB_OK);
  62.     return FALSE;
  63.   }
  64.  
  65.   /*
  66.     Add each button to the drawing-tools toolbox
  67.   */
  68.   hButtonTool = (HWND) 
  69.     SendMessage(hWndTool, TBM_CREATEBUTTON, ID_PENCIL, (LONG)(LPSTR)"Pencil");
  70.   SendMessage(hWndTool, TBM_CREATEBUTTON, ID_LINE, (LONG)(LPSTR)"Line");
  71.   SendMessage(hWndTool, TBM_CREATEBUTTON, ID_OPENRECT,(LONG)(LPSTR)"OpenRect");
  72.   SendMessage(hWndTool, TBM_CREATEBUTTON, ID_FILLRECT,(LONG)(LPSTR)"FillRect");
  73.   SendMessage(hWndTool, TBM_CREATEBUTTON, ID_CIRCLE, (LONG)(LPSTR)"Circle");
  74.   SendMessage(hWndTool, TBM_CREATEBUTTON, ID_FILLCIRC,(LONG)(LPSTR)"FillCirc");
  75.   SendMessage(hWndTool, TBM_CREATEBUTTON, ID_SELECT,  (LONG)(LPSTR)"Net");
  76.   SendMessage(hWndTool, TBM_CREATEBUTTON, ID_FLOOD,   (LONG)(LPSTR)"Floodfill");
  77.  
  78.   /*
  79.     Create the color palette toolbox
  80.   */
  81.   hWndColor = CreateWindow("ToolBox",
  82.                           (LPSTR) "Colors",
  83.                           WS_CHILD | WS_BORDER | WS_CAPTION | 
  84.                           WS_OVERLAPPED | WS_VISIBLE | WS_CLIPSIBLINGS,
  85.                           176, 272,
  86.                           ToolBox.cxButton*8 + 2*GetSystemMetrics(SM_CXBORDER),
  87.                           ToolBox.cyButton*2 + GetSystemMetrics(SM_CYCAPTION)
  88.                                              + 2*GetSystemMetrics(SM_CYBORDER),
  89.                           hWnd,
  90.                           200,
  91.                           hInst,
  92.                           (LPSTR) &ColorBox);
  93.   if (!hWndColor)
  94.   {
  95.     MessageBox(hWnd, "Can't create hWndColor", NULL, MB_OK);
  96.     return FALSE;
  97.   }
  98.  
  99.   /*
  100.     Add the buttons to the palette toolbox
  101.   */
  102.   hButtonColor = (HWND) 
  103.     SendMessage(hWndColor, TBM_CREATEBUTTON, ID_COLOR0,  MAKELONG(0, 0));
  104.   for (i = 1;  i <= 15;  i++)
  105.     SendMessage(hWndColor, TBM_CREATEBUTTON, ID_COLOR0 + i, MAKELONG(0, i));
  106.  
  107.   ShowWindow(hWnd, SW_NORMAL);
  108.   UpdateWindow(hWnd);
  109.  
  110.   /*
  111.     Set the pencil and color 0 as the startup choices
  112.   */
  113.   SendMessage(hWndTool,  WM_COMMAND, ID_PENCIL, 
  114.               MAKELONG(hButtonTool, BN_CLICKED));
  115.   SendMessage(hWndColor, WM_COMMAND, ID_COLOR0, 
  116.               MAKELONG(hButtonColor, BN_CLICKED));
  117.  
  118.   return TRUE;
  119. }
  120.  
  121.  
  122. /****************************************************************************/
  123. /*                                                                          */
  124. /* Function : ToolBoxWndProc()                                              */
  125. /*                                                                          */
  126. /* Purpose  : Window proc for the toolbox class.                            */
  127. /*                                                                          */
  128. /* Returns  :                                                               */
  129. /*                                                                          */
  130. /****************************************************************************/
  131. LONG FAR PASCAL ToolBoxWndProc(hWnd, message, wParam, lParam)
  132.   HWND hWnd;
  133.   unsigned message;
  134.   WORD wParam;
  135.   LONG lParam;
  136. {
  137.   LPDRAWITEMSTRUCT lpDIS;
  138.   LPCREATESTRUCT   lpCS;
  139.   LPTOOLBOX        lpTB, lpSrcTB;
  140.   HANDLE           hTB;
  141.   char             szBuf[64];
  142.   HWND             hButton;
  143.   HICON            hIcon;
  144.   WORD             i;
  145.  
  146.  
  147.   switch (message)
  148.   {
  149.     case WM_CREATE        :
  150.       /*
  151.         When the toolbox is created, we must allocate a TOOLBOX structure
  152.         and attach it to the window. We copy the TOOLBOX structure which
  153.         was passed in the lpCreateParams over to this private data area.
  154.       */
  155.       hTB = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) sizeof(TOOLBOX));
  156.       SetWindowWord(hWnd, 0, hTB);
  157.       lpTB = (LPTOOLBOX) GlobalLock(hTB);
  158.       lpSrcTB = (LPTOOLBOX) ((LPCREATESTRUCT) lParam)->lpCreateParams;
  159.       _fmemcpy(lpTB, (LPSTR) lpSrcTB, sizeof(TOOLBOX));
  160.       lpTB->nButtons = 0;
  161.       GlobalUnlock(hTB);
  162.       break;
  163.  
  164.  
  165.     case TBM_CREATEBUTTON :
  166.       /*
  167.         wParam is the id of the button
  168.         lParam is a far pointer to the icon name or to a color
  169.       */
  170.       hTB = GetWindowWord(hWnd, 0);
  171.       lpTB = (LPTOOLBOX) GlobalLock(hTB);
  172.  
  173.       /*
  174.         Create a button window.
  175.       */
  176.       hButton = CreateWindow("button",
  177.                              NULL, /* (LPSTR) HIWORD(lParam),*/
  178.                              WS_CHILD | BS_PUSHBUTTON | BS_OWNERDRAW,
  179.                              lpTB->nButtons / lpTB->nRows * lpTB->cxButton,
  180.                              lpTB->nButtons % lpTB->nRows * lpTB->cyButton,
  181.                              lpTB->cxButton,
  182.                              lpTB->cyButton,
  183.                              hWnd,
  184.                              wParam,
  185.                              hInst,
  186.                              NULL);
  187.  
  188.       /*
  189.         Load the drawing-tool icon or attach a color value.
  190.       */
  191.       if (LOWORD(lParam) != 0)
  192.         lpTB->toolInfo[lpTB->nButtons].hData = LoadIcon(hInst, (LPSTR)lParam);
  193.       else
  194.         lpTB->toolInfo[lpTB->nButtons].hData = HIWORD(lParam);
  195.  
  196.       /*
  197.         Attach the control id and a tool style
  198.       */
  199.       lpTB->toolInfo[lpTB->nButtons].idTool = wParam;
  200.       lpTB->toolInfo[lpTB->nButtons].wToolStyle =
  201.                        (LOWORD(lParam) == 0) ? TOOL_COLOR : TOOL_ICON;
  202.       lpTB->nButtons++;
  203.  
  204.       ShowWindow(hButton, SW_SHOW);
  205.       GlobalUnlock(hTB);
  206.       return hButton;
  207.  
  208.  
  209.     case WM_COMMAND :
  210.       hTB = GetWindowWord(hWnd, 0);
  211.       lpTB = (LPTOOLBOX) GlobalLock(hTB);
  212.  
  213.       /*
  214.         If a button was clicked, then it should be the currently
  215.         selected tool.
  216.       */
  217.       if (HIWORD(lParam) == BN_CLICKED)
  218.       {
  219.         hButton = lpTB->hwndSelectedTool;
  220.         lpTB->hwndSelectedTool = LOWORD(lParam);
  221.         lpTB->idSelectedTool = wParam;
  222.         if (hButton)  /* redraw the old tool in an unselected state */
  223.         {
  224.           InvalidateRect(hButton, (LPRECT) NULL, TRUE);
  225.           UpdateWindow(hButton);
  226.         }
  227.         InvalidateRect(lpTB->hwndSelectedTool, (LPRECT) NULL, TRUE);
  228.         UpdateWindow(lpTB->hwndSelectedTool);
  229.       }
  230.  
  231.       GlobalUnlock(hTB);
  232.       SendMessage(GetParent(hWnd), WM_COMMAND, wParam, lParam);
  233.       break;
  234.  
  235.     case WM_DRAWITEM :
  236.       hTB = GetWindowWord(hWnd, 0);
  237.       lpTB = (LPTOOLBOX) GlobalLock(hTB);
  238.       lpDIS = (LPDRAWITEMSTRUCT) lParam;
  239.  
  240.       /*
  241.         We have the id of the button. Map that into the proper button
  242.         structure.
  243.       */
  244.       for (i = 0;  i < lpTB->nButtons;  i++)
  245.         if (lpTB->toolInfo[i].idTool == lpDIS->CtlID)
  246.           break;
  247.  
  248.       /*
  249.         Draw the button
  250.       */
  251.       if (i < lpTB->nButtons)
  252.         DrawUserButton(lpDIS->hwndItem, lpDIS->hDC,
  253.                        &lpDIS->rcItem, lpDIS->CtlID,
  254.                        lpDIS->itemState,
  255.                        lpTB->toolInfo[i].hData,
  256.                        lpTB->toolInfo[i].wToolStyle,
  257.                        (lpDIS->CtlID == lpTB->idSelectedTool));
  258.  
  259.       GlobalUnlock(hTB);
  260.       break;
  261.  
  262.  
  263.     case WM_DESTROY:
  264.       hTB = GetWindowWord(hWnd, 0);
  265.       /*
  266.         ==> We should destroy the icon too!
  267.       */
  268.       GlobalFree(hTB);
  269.       break;
  270.  
  271.     default:
  272.       return DefWindowProc(hWnd, message, wParam, lParam);
  273.   }
  274.   return NULL;
  275. }
  276.  
  277.  
  278. /****************************************************************************/
  279. /*                                                                          */
  280. /* Function : DrawUserButton()                                              */
  281. /*                                                                          */
  282. /* Purpose  : Called in response to the WM_DRAWITEM message in order to     */
  283. /*            render a toolbox button.                                      */
  284. /*                                                                          */
  285. /* Returns  : Blah.                                                         */
  286. /*                                                                          */
  287. /****************************************************************************/
  288. VOID PASCAL DrawUserButton(HWND hWnd, HDC hDC, LPRECT lpRect, int idCtrl,
  289.                     WORD iState, HANDLE hData, WORD wStyle, BOOL bCurrent)
  290. {
  291.   HDC     hMemDC;
  292.   RECT    rc;
  293.   HBITMAP hBitmap, hOldBitmap;
  294.   BITMAP  bm;
  295.   extern  BYTE rgb[16][3];
  296.  
  297.   /*
  298.     The rect should be 0,0,39,39.
  299.   */
  300.   CopyRect((LPRECT) &rc, lpRect); 
  301.   FillRect(hDC, (LPRECT) &rc,
  302.            (HBRUSH) GetStockObject(bCurrent ? BLACK_BRUSH : WHITE_BRUSH));
  303.  
  304.   /*
  305.     We want to use 0,0,37,37 to draw the frame.
  306.   */
  307.   InflateRect((LPRECT) &rc, -1, -1);   /* 1,1,38,38 */
  308.   if (iState & ODS_SELECTED)
  309.     OffsetRect((LPRECT) &rc, 1, 1); 
  310.   else
  311.     OffsetRect((LPRECT) &rc, -1, -1);  /* 0,0,37,37 */
  312.   FrameRect(hDC, (LPRECT) &rc, (HBRUSH) GetStockObject(BLACK_BRUSH));
  313.  
  314.   /*
  315.     Now, draw a black box inside the rect at 2,2,36,36
  316.   */
  317.   rc.left += 2;  rc.top += 2; rc.bottom -= 1; rc.right -= 1;
  318.   FillRect(hDC, (LPRECT) &rc, (HBRUSH) GetStockObject(GRAY_BRUSH));
  319.  
  320.   /*
  321.     Now, draw a gray  box inside the rect at 2,2,34,34
  322.   */
  323.   rc.right -= 2;  rc.bottom -= 2;
  324.   FillRect(hDC, (LPRECT) &rc, (HBRUSH) GetStockObject(LTGRAY_BRUSH));
  325.  
  326.  
  327.   if (wStyle == TOOL_ICON)
  328.     /*
  329.       Draw the icon inside of the button
  330.     */
  331.     DrawIcon(hDC, rc.left, rc.top, (HICON) hData);
  332.   else if (wStyle == TOOL_COLOR)
  333.   {
  334.     /*
  335.       Fill a color button with a solid colored rectangle
  336.     */
  337.  
  338.     HBRUSH hBrush, hOldBrush;
  339.  
  340.     hBrush = CreateSolidBrush(RGB(rgb[hData][2], rgb[hData][1], rgb[hData][0]));
  341.     hOldBrush = SelectObject(hDC, hBrush);
  342.     InflateRect((LPRECT) &rc, -1, -1);
  343.     FillRect(hDC, (LPRECT) &rc, hBrush);
  344.     SelectObject(hDC, hOldBrush);
  345.     DeleteObject(hBrush);
  346.   }  
  347. }
  348.